// users/pages/user/userinfo/userinfo.js
var app = getApp()
var globalData = app.globalData
Page({
  replyPhoto: function () {
    var that = this;
    wx.chooseMedia({
      count: 1,
      success: function (res) {
        var filePath = res.tempFiles[0].tempFilePath;
        //console.log(filePath)
        that.setData({
          profilePhotoPath: filePath,
          profilePhotoHidden: false
        });
        //前后端协作部分
        wx.uploadFile({
          filePath: that.data.profilePhotoPath,
          name: 'userAvatar',
          url: globalData.url + 'users/0/',
          header: {
            "Authorization": globalData.token,
            'Content-type': 'multipart/form-data'
          },
          formData: {},
          success: () => {
            wx.showToast({
              title: '上传成功',
              icon: "success",
              duration: 1000,
            })
          },
          fail: () => {
            wx.showToast({
              title: '上传失败',
              icon: "error",
              duration: 1000,
            })
          }
        })
      },
      fail: function (error) {
        console.error("调用文件时出错"),
          console.warn(error),
          wx.showToast({
            title: '调用文件失败',
            icon: 'error',
            duration: 1000,
          })
      },
      complete: function () {

      }
    })
  },
  replyContact() {
    //前后端协作部分
    wx.showActionSheet({
      itemList: ['微信：' + this.data.userInfo.WeChat, 'QQ：' + this.data.userInfo.QQ],
      itemColor: "#000000",
    }).then((res) => {
      if (res.tapIndex) {
        wx.showModal({
          title: '请输入您qq号',
          editable: true,
          placeholderText: '请输入',
          success: async (res) => {
            if (res.confirm) {
              let info = this.data.userInfo
              info.QQ = res.content
              let put = await app.put('user/', info)
              this.getInfo()
            }
          }
        })
      } else {
        wx.showModal({
          title: '请输入您微信号',
          editable: true,
          placeholderText: '请输入',
          success: async (res) => {
            if (res.confirm) {
              let info = this.data.userInfo
              info.WeChat = res.content
              let put = await app.put('user/', info)
              this.getInfo()
            }
          }
        })
      }
    })
  },
  replyName: function () {
    wx.showModal({
      title: "请输入用户名",
      editable: true,
      placeholderText: "一个有趣的灵魂",
      success: async (res) => {
        if (res.confirm) {
          let info = this.data.userInfo
          info.nickname = res.content
          console.log(info)
          let post = await app.put('user/', info)
          this.getInfo()
          wx.showToast({
            title: '修改成功',
            icon: "success",
            duration: 1000,
          })
        }
      }
    })
  },

  /**
   * 页面的初始数据
   */
  data: {
    profilePhotoPath: '',
    profilePhotoHidden: false,
    inputNameValue: '',
    wechat: '',
    qq: ''
  },

  //获取用户信息
  async getInfo() {
    let res = await app.get('user/', {})
    delete res.data.avatar
    this.setData({
      userInfo: res.data
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})